package cn.hfjava.kz.utils;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Component
public class SendMessage {
    @Value("${sms.aliyun.accessKeyId}")
    private static   String ki;
    @Value("${sms.aliyun.accessKeySecret}")
    private  String ks;

    public static void main(String[] args) {
        System.out.println(ki);
    }

    public void senMsessage(String phone, String code) {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI5tHy7ptd8ycYVLVBm85S")
                .accessKeySecret("2XtWsKuZyXV90ZnOesoWDCbTMufhLm")
                .build());
        AsyncClient client = AsyncClient.builder()
                .region("cn-shanghai") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();
        /*SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("阿里云短信测试")
                .templateCode("SMS_154950909")
                .phoneNumbers("18895670039")
                .templateParam("{\"code\":\"1234\"}")
                .build(); */
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("云审批系统")
                .templateCode("SMS_469000009")
                .phoneNumbers(phone)
                .templateParam("{\"code\":" + code + "}")
                .build();
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = null;
        try {
            resp = response.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        System.out.println("================================================" + new Gson().toJson(resp));
        client.close();
    }
}
